package cc.mrbird.management.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.ExcelUtils;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.grid.domain.GridInfo;
import cc.mrbird.management.domain.ManagementInstitutions;
import cc.mrbird.management.service.ManagementInstitutionsService;
import cc.mrbird.system.domain.Dict;
import cc.mrbird.system.domain.User;
import cc.mrbird.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Api(description = "管理机构接口")
@Controller
public class ManagementInstitutionsController extends BaseController{
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ManagementInstitutionsService managementInstitutionsService;

    @Log("获取管理机构信息")
    @RequestMapping(value = "management",method = RequestMethod.POST)
   /* @RequiresPermissions("management:list")*/
    public String index() {
        return "management/management/management";
    }
    @Log("获取管理机构信息")
    @ApiOperation(value = "获取管理机构信息" ,  notes="获取管理机构信息")
    @RequestMapping(value = "management/list",method = RequestMethod.POST)
    /*@RequiresPermissions("management:list")*/
    @ResponseBody
    public Map<String, Object> managementList(QueryRequest request,ManagementInstitutions managementInstitutions) {
        return super.selectByPageNumSize(request, () -> this.managementInstitutionsService.findManaList(managementInstitutions, request));
    }

    @ApiOperation(value = "导出Excel")
    @RequestMapping(value = "management/excel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementExcel(ManagementInstitutions managementInstitutions){
        try {
            List<ManagementInstitutions> list = this.managementInstitutionsService.findManaList(managementInstitutions, null);
            return FileUtil.createExcelByPOIKit("管理机构表", list, ManagementInstitutions.class);
        } catch (Exception e) {
            log.error("导出管理机构表Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导出csv")
    @RequestMapping(value = "management/csv",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementCsv(ManagementInstitutions managementInstitutions) {
        try {
            List<ManagementInstitutions> list = this.managementInstitutionsService.findManaList(managementInstitutions, null);
            return FileUtil.createCsv("管理机构表", list, ManagementInstitutions.class);
        } catch (Exception e) {
            log.error("导出管理机构表Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导入excel")
    @RequestMapping(value = "management/import",method = RequestMethod.POST)
    public ResponseBo eximport(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = managementInstitutionsService.batchImport(fileName, file);
            return  ResponseBo.ok(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseBo.error("导入数据失败,请重新检查excel数据导入");
    }


    @RequestMapping(value = "management/getMana" , method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo getMana(@ApiParam(name = "id",value="管理机构id")Long id) {
        try {
            ManagementInstitutions managementInstitutions = this.managementInstitutionsService.findById(id);
            return ResponseBo.ok(managementInstitutions);
        } catch (Exception e) {
            log.error("获取管理机构信息失败", e);
            return ResponseBo.error("获取管理机构信息失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "管理机构添加")
    @RequestMapping(value = "management/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo addMana(ManagementInstitutions managementInstitutions) {
        try {
            ManagementInstitutions result = this.managementInstitutionsService.findByName(managementInstitutions.getMiName());
            if (result != null) {
                return ResponseBo.warn("该用户名已被使用！");
            }
            this.managementInstitutionsService.add(managementInstitutions);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }

    @ApiOperation(value = "修改管理机构信息")
    @Log("修改管理机构信息")
   /* @RequiresPermissions("management:update")*/
    @RequestMapping(value = "management/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo updateMana(ManagementInstitutions managementInstitutions) {
        try {
            this.managementInstitutionsService.updateMana(managementInstitutions);
            return ResponseBo.ok("修改管理机构成功！");
        } catch (Exception e) {
            log.error("修改管理机构失败", e);
            return ResponseBo.error("修改管理机构失败，请联系网站管理员！");
        }
    }

    @Log("删除管理机构")
    /*@RequiresPermissions("management:delete")*/
    @RequestMapping(value = "management/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo deleteManagement(@ApiParam(value="管理机构id字符串")String ids) {
        try {
            this.managementInstitutionsService.deleteMana(ids);
            return ResponseBo.ok("删除管理机构成功！");
        } catch (Exception e) {
            log.error("删除管理机构失败", e);
            return ResponseBo.error("删除管理机构失败，请联系网站管理员！");
        }
    }



}
